package com.itheima.travel.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.travel.constant.RedisConstant;
import com.itheima.travel.mapper.FavoriteMapper;
import com.itheima.travel.mapper.RouteMapper;
import com.itheima.travel.pojo.Route;
import com.itheima.travel.pojo.RouteExample;
import com.itheima.travel.req.AffixVo;
import com.itheima.travel.req.RouteVo;
import com.itheima.travel.service.AffixService;
import com.itheima.travel.service.RedisCacheService;
import com.itheima.travel.service.RouteService;
import com.itheima.travel.session.SubjectSellerContext;
import com.itheima.travel.utils.BeanConv;
import com.itheima.travel.utils.EmptyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RouteServiceImpl implements RouteService {
    //注入dao层线路
    @Autowired
    private RouteMapper routeMapper;
    //注入商家session
    @Autowired
    private SubjectSellerContext subjectSellerContext;
    //注入收藏的信息
    @Autowired
    private FavoriteMapper favoriteMapper;
    //缓存数据库
    @Autowired
    private RedisCacheService redisCacheService;
    @Autowired
    private AffixService affixService;
    //添加线路
    @Override
    public Integer addRoute(RouteVo routeVo) {
        //获取要添加的信息,并返回添加成功影响的行数
        int insert = routeMapper.insert(BeanConv.toBean(routeVo, Route.class));
        return insert;
    }

    //根据id查询
    //需要放入redis缓存数据库中
    @Override
    public RouteVo findRouteById(RouteVo routeVo) {
        //根据key 生成唯一标识来查找数据
        String key= RedisConstant.ROUTESERVICE_FINDROUTEBYID+routeVo.getId();
       //根据id查找信息并返回结果
        return redisCacheService.singleCache(()->{
            RouteVo resultVo = BeanConv.toBean(routeMapper.selectByPrimaryKey(routeVo.getId()), RouteVo.class);
            //通过缓存数据找到图片信息
            AffixVo affixVo = AffixVo.builder().businessId(resultVo.getId()).build();
            //根据例子条件查询出说有的图片信息，并设置到AffixVo中
            resultVo.setAffixVoList(affixService.findAffixBuBusinessId(affixVo));
            return resultVo;
        },key);
    }
    //分页查找
    @Override
    public PageInfo<RouteVo> findRouteByPage(RouteVo routeVo, int pageNumber, int pageSize) {

        //设置分页查询的当前页和每页显示条数
        PageHelper.startPage(pageNumber,pageSize);
        //创建一个例子，条件的意思
        RouteExample routeExample = new RouteExample();
        //创建一个规范
        RouteExample.Criteria criteria = routeExample.createCriteria();
        if(!EmptyUtil.isNullOrEmpty(routeVo.getCategoryId())){
            //如果线路表中de分类模块id不为空，则拼接上去
            criteria.andCategoryIdEqualTo(routeVo.getCategoryId());
        }
        if(!EmptyUtil.isNullOrEmpty(routeVo.getRouteName())){
            //如果线路名称不为空则拼接
            criteria.andRouteNameLike("%"+routeVo.getRouteName());
        }
        //价格高于最小值，大于最大值，拼接
        if(!EmptyUtil.isNullOrEmpty(routeVo.getMinPrice())){
            //如果价格不为空则拼接
            criteria.andPriceGreaterThan(routeVo.getMinPrice());
        }
        if(!EmptyUtil.isNullOrEmpty(routeVo.getMaxPrice())){
            //最高价格不为空
            criteria.andPriceLessThan(routeVo.getMaxPrice());
        }
        //根据例子查询
        List<Route> routeList = routeMapper.selectByExample(routeExample);
        //创建一个pageInfo，将集合放进去
        PageInfo<Route> routePageInfo = new PageInfo<>(routeList);
        //创建一个routeVo类型的
        PageInfo<RouteVo> routeVoPageInfo = new PageInfo<>();
        //将pojo对象转为vo对象
        BeanConv.toBean(routePageInfo,routeVoPageInfo);
        System.out.println(routeVoPageInfo);
        //结果集转换
        List<RouteVo> routeVoList = BeanConv.toBeanList(routeList, RouteVo.class);
        //将vo类型的数据封装到集合中
        routeVoPageInfo.setList(routeVoList);
        //处理图片
        for (RouteVo vo : routeVoList) {
            AffixVo affixVo = AffixVo.builder().businessId(vo.getId()).build();
            vo.setAffixVoList(affixService.findAffixBuBusinessId(affixVo));
        }
        return routeVoPageInfo;
    }

    //添加线路,引入缓存
    @Override
    public Integer updateRoute(RouteVo routeVo) {
        //判断线路id是否为空，防止空指针异常
        if(EmptyUtil.isNullOrEmpty(routeVo.getId())){
            //如果为空，则修改不成功
            return 0;
        }
        //否则返回1，表示修改成功
        int i = routeMapper.updateByPrimaryKey(BeanConv.toBean(routeVo, Route.class));
        if(i>0){
            //表示修改成功
            //删除线路在redis中的值
            redisCacheService.deleSingleCache(RedisConstant.ROUTESERVICE_FINDROUTEBYID+routeVo.getId());
            //删除照片的
            redisCacheService.deleListCache(RedisConstant.AFFIXSERVICE_FINDAFFIXBYBUSINESSID+routeVo.getId());
        }
        return i;
    }
}
